import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Firefox()

driver.get('http://129.211.129.101:9007/index.php?m=admin&c=public&a=login')

driver.implicitly_wait(10)
driver.find_element(By.NAME, 'username').send_keys('admin')
driver.find_element(By.NAME, 'userpass').send_keys('password')
driver.find_element(By.NAME, 'userverify').send_keys('1234')
driver.find_element(By.CLASS_NAME, 'Btn').click()

driver.find_element(By.LINK_TEXT, '商品管理').click()
driver.find_element(By.LINK_TEXT, '添加商品').click()

# 输入商品名称,用iframe标签name属性名
driver.switch_to.frame('mainFrame')
driver.find_element(By.NAME, 'name').send_keys('iphone 16')

# 选择分类
# WebDriverWait(driver, 10, 0.5).until(expected_conditions.visibility_of_element_located((By.ID, '1'))).click()
time.sleep(3)
driver.find_element(By.ID, '1').click()
driver.find_element(By.ID, '2').click()
driver.find_element(By.ID, '6').click()
# 双击
ActionChains(driver).double_click(driver.find_element(By.ID, '7')).perform()

# 选择品牌
brand = driver.find_element(By.NAME, 'brand_id')
Select(brand).select_by_value('1')

# 添加图片
driver.find_element(By.LINK_TEXT, '商品图册').click()
driver.find_element(By.NAME, 'file').send_keys('C:\\Users\\admin\Desktop\测试数据\商品.png')
driver.find_element(By.CSS_SELECTOR, '.uploadBtn.state-finish.state-ready').click()

# 处理弹框
WebDriverWait(driver, 30, 0.5).until(expected_conditions.alert_is_present())
driver.switch_to.alert.accept()

driver.find_element(By.CLASS_NAME, 'button_search').click()

time.sleep(5)
driver.quit()
